//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client;

import tv.dyndns.kishibe.client.packet.PacketMatchingPlayer;
import tv.dyndns.kishibe.client.util.SerialTimer;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class SceneMatching implements SceneBase {
	private PanelMatching panel;
	private final boolean addPenalty;

	public SceneMatching(boolean addPenalty) {
		this.addPenalty = addPenalty;
		panel = new PanelMatching(this);

		Controller.getInstance().setGamePanel(panel);
	}

	private final Timer timerUpdate = new SerialTimer() {
		public void run() {
			update();
		}
	};

	private void update() {
		final int sessionId = SharedData.getInstance().getSessionId();
		final int playerListId = SharedData.getInstance().getPlayerListIndex();

		// マッチング待ち時間の取得
		Service.Util.getInstance().waitForMatching(sessionId, playerListId, callbackWaitForMatching);

		// ほかのプレイヤー情報の取得
		Service.Util.getInstance().getMachingPlayer(sessionId, callbackGetMachingPlayer);
	}

	private final AsyncCallback<Integer> callbackWaitForMatching = new AsyncCallback<Integer>() {
		public void onSuccess(Integer result) {
			if (result <= 0) {
				Controller.getInstance().setScene(new SceneReadyForGame(addPenalty));
			} else {
				panel.setRestSecond(result);
			}
		}

		public void onFailure(Throwable caught) {
			Controller.getInstance().showErrorMessage("SceneMatching.update() - マッチングに失敗しました(1)。");
		}
	};

	private final AsyncCallback<PacketMatchingPlayer[]> callbackGetMachingPlayer = new AsyncCallback<PacketMatchingPlayer[]>() {
		public void onSuccess(PacketMatchingPlayer[] result) {
			if (result == null) {
				Controller.getInstance().showErrorMessage("SceneMatching.callbackGetMachingPlayer - 無効なマッチング情報が返されました");
			}

			panel.setPlayerList(result);
		}

		public void onFailure(Throwable caught) {
			Controller.getInstance().showErrorMessage("SceneMatching.update() - マッチングに失敗しました(2)。");
		}
	};

	public void requestSkip() {
		final int sessionId = SharedData.getInstance().getSessionId();
		final int playerListId = SharedData.getInstance().getPlayerListIndex();

		Service.Util.getInstance().requestSkip(sessionId, playerListId, callbackRequestSkip);
	}

	private final AsyncCallback<Integer> callbackRequestSkip = new AsyncCallback<Integer>() {
		public void onSuccess(Integer result) {
		}

		public void onFailure(Throwable caught) {
			new SerialTimer() {
				public void run() {
					requestSkip();
				}
			}.schedule(1000);

			Controller.getInstance().showErrorMessage("PanelMatching.requestSkip() - マッチングに失敗しました(3)。パケットを再送します。");
		}
	};

	public void onLoad() {
		timerUpdate.scheduleRepeating(1000);
	}

	public void onUnload() {
		timerUpdate.cancel();
	}
}
